package com.example.tired1.zyfypt_14120200131_syn.model;

import com.example.tired1.zyfypt_14120200131_syn.bean.Constant;
import com.example.tired1.zyfypt_14120200131_syn.bean.UserBean;
import com.example.tired1.zyfypt_14120200131_syn.iface.LoginLister;
import com.example.tired1.zyfypt_14120200131_syn.service.LoginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by tired1 on 2017/5/9.
 */

public class LoginMode {
    Retrofit retrofit;


    public  LoginMode(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())//把返回结果转换为。。。
                .build();

    }
    public void login(String username , String password,final LoginLister loginLister){
        final LoginService retrofitSerivce=retrofit.create(LoginService.class);
        Call<UserBean> call=retrofitSerivce.login(username,password);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.body()!=null && response.isSuccessful()){
                    loginLister.onResponse(response.body());


                }
                else {
                    System.out.println("==>>"+"response fall!");
                    loginLister.onFail("onResponse fail");//使用了Java的回调机制

                }

            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                System.out.println("==>>"+"response onFail!");
                loginLister.onFail(t.toString());


            }
        });
    }


}
